package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dto.EmployeeSalaryGroupDto;
import com.zhangtai.modules.entity.salary.SalaryDistributionRecordingEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 薪资组分配记录
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-09-22 14:01:18
 */
@Mapper
public interface SalaryDistributionRecordingDao extends BaseMapper<SalaryDistributionRecordingEntity> {

    @Select(
            "<script>SELECT\n" +
                    "\ttsd.*,\n" +
                    "\ttei.NAME,\n" +
                    "\ttei.org_id,\n" +
                    "\ttei.orgname,\n" +
                    "\ttei.unit_id,\n" +
                    "\ttei.unitname,\n" +
                    "\ttei.CODE,\n" +
                    "\ttei.jobname,\n" +
                    "\ttei.job_id \n" +
                    "FROM\n" +
                    "\ttb_salary_distribution_recording tsd\n" +
                    "\tLEFT JOIN tb_employee_info tei ON tsd.oid = tei.oid <where> tsd.is_delete = 0 " +
                    "<if test='name!=null'> and tei.name like '%${name}%' </if>" +
                    "<if test='code!=null'> and tei.code like '%${code}%' </if>" +

                    "<if test='orgIds!=null and orgIds.size>0'> and tsd.unit_id in " +
                    " <foreach item=\"orgIds\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{orgIds}</foreach>" +
                    "</if>" +
                    "<if test='orgIds==null or orgIds.size==0'> and tsd.unit_id = null </if>" +

                    "<if test='distributionTime!=null'> and DATE_FORMAT(tsd.distribution_time,'%Y-%m') = #{distributionTime} </if>" +
//                    "<if test='orgId!=null and orgId.size>0'> and tei.org_id in " +
//                    " <foreach item=\"orgId\" index=\"index\" collection=\"orgId\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
//                    "</if>" +
//                    "<if test='orgId==null or orgId.size==0'> and tei.org_id = null </if>" +
//
//                    "<if test='unitIds!=null and unitIds.size>0'> and tei.unit_id in " +
//                    " <foreach item=\"unitIds\" index=\"index\" collection=\"unitIds\" open=\"(\" separator=\",\" close=\")\">#{unitIds}</foreach>" +
//                    "</if>" +
//                    "<if test='unitIds==null or unitIds.size==0'> and tei.unit_id = null </if>" +
//
//                    "<if test='jobIds!=null and jobIds.size>0'> and tei.job_id in " +
//                    " <foreach item=\"jobIds\" index=\"index\" collection=\"jobIds\" open=\"(\" separator=\",\" close=\")\">#{jobIds}</foreach>" +
//                    "</if>" +
//                    "<if test='jobIds==null or jobIds.size==0'> and tei.job_id = null </if>" +

                    "</where>  ORDER BY tsd.create_time DESC " +
                    //"</where>  ORDER BY tds.create_time DESC ,tds.status asc " +
                    "</script>"
    )
    List<Map<String, Object>> getSalaryDistributionRecordingInfoList(
            @Param("orgIds")List<Long> orgIds,
            @Param("orgId")List<Long> orgId,
            @Param("unitIds")List<Long> unitIds,
            @Param("jobIds")List<Long> jobIds,
            @Param(value = "distributionTime") String distributionTime,
            @Param(value = "name") String name,
            @Param(value = "code") String code,
            Page<EmployeeSalaryGroupDto> page
    );

}
